﻿using Face8280WebSocketServer.Command;
using Face8280WebSocketServer.Command.Models;
using Face8280WebSocketServer.Command.WorkParameter;
using Face8280WebSocketServer.DB.Device;
using Face8280WebSocketServer.DB.TimeGroup;
using Face8280WebSocketServer.Interface;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;

namespace Face8280WebSocketServer.Service
{
    /// <summary>
    /// 开门时段
    /// </summary>
    public class TimeGroupService : BaseService, ITimeGroupService
    {
        ICacheService _CacheService;

        public TimeGroupService(DbContext context, ICacheService cache) : base(context)
        {
            _CacheService = cache;
        }


        public void IniTimeGroupDB()
        {
            Excute("Delete from TimeGroup");
            List<TimeGroupDetail> lst = new List<TimeGroupDetail>();

            for (int i = 0; i < 64; i++)
            {
                lst.Add(new TimeGroupDetail(i + 1));

            }
            var detail = lst[0];
            detail.Week1 = "00:00--23:59/00:00--00:00/00:00--00:00/00:00--00:00/00:00--00:00/00:00--00:00/00:00--00:00/00:00--00:00";
            detail.Week2 = detail.Week1;
            detail.Week3 = detail.Week1;
            detail.Week4 = detail.Week1;
            detail.Week5 = detail.Week1;
            detail.Week6 = detail.Week1;
            detail.Week7 = detail.Week1;

            DetachAll();
            Insert<TimeGroupDetail>(lst);

            UpdateAllDevice();

        }

        /// <summary>
        /// 更新所有设备的待上传状态
        /// </summary>
        private async void UpdateAllDevice()
        {
            //Excute("Update FaceDevice set UploadStatus = 0;");
            var devicequrey = from d in Set<FaceDeviceInfo>()
                              select d.DeviceSn;
            var device = _CacheService.GetDevices();
            var list = GetAll();
            List<GroupDetail> TimeGroups = new List<GroupDetail>();
            list.ForEach(a =>
            {
                TimeGroups.Add(FilterGroup(a));
            });
            foreach (var d in device)
            {
                if (devicequrey.Contains(d.DeviceSn))
                {
                    var dto = new SetWorkParam_DTO();
                    dto.TimeGroups = TimeGroups;
                    var commandDetail = await Allocator.GetCommandDetail(d.DeviceSn);
                    var cmd = new SetWorkParameter(commandDetail, dto);
                    await Allocator.AddCommandAsync(cmd);
                    var result = cmd.getResult() as SetWorkParameterResult;
                }
            }


        }
        static string DefaultWeek = "00:00--00:00/00:00--00:00/00:00--00:00/00:00--00:00/00:00--00:00/00:00--00:00/00:00--00:00/00:00--00:00";
        private GroupDetail FilterGroup(TimeGroupDetail time)
        {
            var result = new GroupDetail();
            result.Num = time.Num;
            string week1 = time.Week1.Equals(DefaultWeek) ? null : time.Week1;
            string week2 = time.Week2.Equals(DefaultWeek) ? null : time.Week2;
            string week3 = time.Week3.Equals(DefaultWeek) ? null : time.Week3;
            string week4 = time.Week4.Equals(DefaultWeek) ? null : time.Week4;
            string week5 = time.Week5.Equals(DefaultWeek) ? null : time.Week5;
            string week6 = time.Week6.Equals(DefaultWeek) ? null : time.Week6;
            string week7 = time.Week7.Equals(DefaultWeek) ? null : time.Week7;

            if (week1 != null || week2 != null || week3 != null || week4 != null || week5 != null || week6 != null || week7 != null)
            {
                result.Time = new WeekModel
                {
                    Week1 = week1,
                    Week2 = week2,
                    Week3 = week3,
                    Week4 = week4,
                    Week5 = week5,
                    Week6 = week6,
                    Week7 = week7,
                };
            }
            return result;
        }

        public List<TimeGroupDetail> GetAll()
        {
            List<TimeGroupDetail> lst = Query<TimeGroupDetail>(p => true).OrderBy(x => x.Num).ToList();
            return lst;
        }

        public void UpdateTimeGroup(TimeGroupDetail detail)
        {
            Update(detail);
            UpdateAllDevice();
        }
    }
}
